<?php 

	class Default_RelatorysController extends System_Controller
	{
        private $projectInfo;
        private $jsonData;

        public function preDispatch()
        {
             $auth = Zend_Auth::getInstance();
             if(!$auth->hasIdentity())
             {
                 $this->_redirect('/user/login');
             }

            $this->projectInfo =  new Zend_Session_Namespace('projectInfo');

            if(isset($_POST['tableAjax']))
            {
                $this->jsonData = $_POST['tableAjax'];
                $this->jsonData = stripslashes($this->jsonData);
                $this->jsonData = json_decode($this->jsonData, true);
            }           
        }

        public function indexAction()
        {
            $this->_helper->layout()->disableLayout();

            if($this->_request->isPost())
            {
                $postData = $this->_request->getPost();

                $table = new Table();
                $result = $table->getByProjectId($postData['projectId']);
                $this->view->tables = $result;
                $this->projectInfo->id = $postData['projectId'];
                $this->view->projectId = $this->projectInfo->id;
            }
            else
            {
                $this->_redirect('/index');  
            }
        }

        public function contentAction()
        {
            //testa se os dados forma passados por JSON
            
            if($this->_request->isPost())
            {
                $postData = $this->_request->getPost();

                $this->_helper->layout()->disableLayout();
                //this->_helper->viewRenderer->setNoRender(true);

                if($postData['action'] == 'showRelatorys')
                {
                    $projectsTables = new ProjectsTables();
                    $tableContent = $projectsTables->getTableMetadataById($postData['tableId']);
                    
                    $table = new Tables($tableContent[0]['name']);
                    $schema = $table->getSchema();

                    $relatorys = new Relatorys();
                    $relatorysResult = $relatorys->getAll();


                    $return = array('schema'=>$schema,
                                    'content'=> $relatorysResult);

                    $this->view->table = $return;
                    $this->view->tableId = $tableContent[0]['id'];
                    $this->view->projectId = $this->projectInfo->id;

                }   
            }
            else
            {
                $this->_redirect('/relatorys/index');
            }
        }

        public function resultAction()
        {

            if($this->_request->isPost())
            {
                $postData = $this->_request->getPost();
                $metaDataArray = explode('_',$postData['tableInfo']);
                $projectsTables = new ProjectsTables();
                $table = $projectsTables->getTableMetadataById($metaDataArray[3]);
                $tableName = $table[0]['name'];

                $result = array();
                foreach($postData as $fieldId => $field)
                {   
                    $fieldIdArray = explode('_',$fieldId);
                    //se o id tem 3 elementos entao é com o banco    
                    if(isset($fieldIdArray[1]) && $field=='on')
                    {  
                        $relatorys = new Relatorys();
                        $result[$fieldIdArray[1]][$fieldIdArray[0]] = $relatorys->getResult($tableName,$fieldIdArray[1],$fieldIdArray[0]);
                    } 
                }   
                $this->view->teste = $result;           
            }
            else
            {
                $this->_redirect('/relatorys/index');
            }
        }   
    }
?>